package com.example.selfcontrol.controller;

import com.example.selfcontrol.config.Response;
import com.example.selfcontrol.dto.UserInfoDTO;
import com.example.selfcontrol.entity.Credit;
import com.example.selfcontrol.entity.User;
import com.example.selfcontrol.service.UserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.RequestHandler;

@RestController
@RequestMapping("/api/users")
@Api(value = "用户视图")
public class UserController {
    @Autowired
    private UserService userService ;

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public Response addUser(@RequestBody User user) {
        return userService.addUser(user);
    }

    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Response loginUser(@RequestBody Credit credit) {
        return userService.loginUser(credit);
    }

    @GetMapping ("/{id}")
    @ApiOperation("通过Id查询用户")
    public User getUser(@PathVariable Integer id) {
        return userService.getUser(id);
    }


    @PutMapping("/auth/{id}")
    @ApiOperation("更新用户信息 | 根据传入接口的userId实现更新")
    public Response updateUser(@RequestBody User user ,@RequestHeader("Authorization") String tokenHeader) {
        String token = tokenHeader.substring(7);
        return userService.updateUser(user,token) ;
    }


    @GetMapping("/info")
    @ApiOperation("侧边栏用户的 粉丝数 | 关注数 | 文章数量")
    public UserInfoDTO getUserInfo(int userId) {
        return userService.getUserInfo(userId) ;
    }


    @GetMapping("/exp/increase")
    @ApiOperation("增加用户经验值")
    public Response IncUserExp(int userId,int incCount) {
        return userService.IncUserExp(userId,incCount) ;
    }

    @GetMapping("/exp/rank")
    @ApiOperation("查询用户段位")
    public String UserRank(int userId) {
        return userService.UserRank(userId) ;
    }


    @GetMapping("/update/articleCount")
    @ApiOperation("更新用户文章数")
    public Response UpdateUserArticleNumber(Long userId) { return userService.UpdateUserArticleNumber(userId) ; }

}
